package date

import (
	"AnniversaryNoticeLite/pkg/log"
	"errors"
	"go.uber.org/zap"
	"time"
)

func DaysBetweenDates(date1, date2 string) (int, error) {
	t1, err := time.Parse(time.DateOnly, date1)
	if err != nil {
		log.Logger.Error("解析第一个日期失败", zap.Error(err), zap.String("date1", date1))
		return 0, errors.New("解析第一个日期失败")
	}

	t2, err := time.Parse(time.DateOnly, date2)
	if err != nil {
		log.Logger.Error("解析第二个日期失败", zap.Error(err), zap.String("date2", date2))
		return 0, errors.New("解析第二个日期失败")
	}

	diff := t2.Sub(t1)

	days := int(diff.Hours() / 24)
	if days < 0 {
		days = -days
	}

	return days, nil
}
